import type { TableProps } from 'antd';
import { Space } from 'antd';
import { Col, Row, Table } from 'antd';
import React, { useState } from 'react';
import styles from './index.less';
import PPPagination from '../PPPagination';

interface PPTableProps {
  dataSource: any[];
  columns: any[];
  onPageChange: (page: number, pageSize: number) => void; // 添加一个新的回调函数
  totalNum: number; // 总数据量，从父组件传递
}

const PPTable: React.FC<PPTableProps & Partial<TableProps<any>>> = (props) => {
  const totalNum = props.dataSource?.length || 0;
  const [pageSize, setPageSize] = useState(10); // 每页展示的条数
  const [currentPage, setCurrentPage] = useState(1); // 当前页码

  const handlePageChange = (page: number, newPageSize?: number) => {
    const updatedPageSize = newPageSize || pageSize;
    setPageSize(updatedPageSize);
    setCurrentPage(page);

    // 调用父组件传入的 onPageChange 回调
    if (props.onPageChange) {
      props.onPageChange(page, updatedPageSize);
    }
  };

  let dataSourceT1 = props.dataSource;
  let dataSourceT2: any[] = [];
  if (props.dataSource) {
    const baseIndex = pageSize * (currentPage - 1);
    let realPageSize = pageSize;
    // Reached last page, split data across two tables
    if (baseIndex + pageSize > totalNum) {
      realPageSize = totalNum - baseIndex + 1;
    }
    dataSourceT1 = props.dataSource?.slice(baseIndex, baseIndex + realPageSize / 2);
    dataSourceT2 = props.dataSource?.slice(baseIndex + realPageSize / 2, baseIndex + realPageSize);
  }

  return (
    <div className={`${styles.table}`}>
      <Row>
        <Col span={24} style={{ borderRight: '0.063rem solid rgba(151,151,151,0.27)' }}>
          <Table {...props} dataSource={props.dataSource} pagination={false} rowSelection={undefined} />
        </Col>
        {/* <Col span={24}>
          <Table {...props} dataSource={dataSourceT2} pagination={false} rowSelection={undefined} />
        </Col> */}
      </Row>
      <Row style={{ marginTop: '1.75rem' }}>
        <Col span={24}>
          <Space align="center">
            <PPPagination
              totalNum={props.totalNum}
              pageSize={pageSize}
              currentPage={currentPage}
              onChange={handlePageChange}
            />
          </Space>
        </Col>
      </Row>
    </div>
  );
};
export default PPTable;
